package com.c.ctgproject.ui.storage.part

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.storage.StoragePartDetailAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.storage.StoragePartsOutEntity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.handlerCall
import com.c.ctgproject.utils.initData
import com.c.ctgproject.utils.showToast
import kotlinx.android.synthetic.main.activity_storage_parts_out_detail.*

/**
 *作者:LiBW
 *创建日期: 2020/9/15
 *描述:仓储 配件出库详情 页面
 */
class StoragePartsOutDetailActivity : BaseActivity() {
    private val mViewModel: StoragePartsOutDetailViewModel by lazy { ViewModelProvider(this)[StoragePartsOutDetailViewModel::class.java] }
    private val data: StoragePartsOutEntity.DataBean.ListBean by lazy { intent.extras!!.get(MyConstants.ENTITY) as StoragePartsOutEntity.DataBean.ListBean }

    override fun getLayout() = R.layout.activity_storage_parts_out_detail

    override fun event() {
        changeTransparentStatusBar()
        mCarCodeTV.text = data.carCode.toString()
        mTimeTV.text = data.auditTime.toString()
        mSellerTV.text = data.salesPersonName.toString()
        data.partsInfos?.let {
            val mAdapter = StoragePartDetailAdapter(this, it)
            mStorageRV.adapter = mAdapter
            mStorageRV.layoutManager = LinearLayoutManager(this)
        }
        mViewModel.confirmResult.observe(this, Observer {
            MyLog.d("确认出库回调:$it")
            loading.dismiss()
            it.handlerCall({
                showToast("确认出库成功")
                finish()
            }, { msg ->
                showToast(msg)
            })
        })
        mViewModel.error.observe(this, Observer {
            loading.dismiss()
            MyLog.d("确认错误回调:$it")
            showToast(it)
        })
    }

    override fun click() {
        //确定出库
        mConfirmBTN.setOnClickListener {
            loading.show()
            mViewModel.confirm( data.partsSalesOrderId)
        }
    }
}